package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeFilter;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;

    // 处理查询所有部门请求
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo) {
        PageInfo<Employee> pageResult = employeeService.query(qo);
        model.addAttribute("pageResult", pageResult);

        model.addAttribute("departments", departmentService.selectAll());
        // 找视图 /WEB-INF/views/employee/list.jsp }
        return "/employee/list";
    }

    // 处理删除请求
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        try {
            if (id != null) {
                employeeService.deleteByPrimaryKey(id);

            }
            return new JsonResult(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false, "删除失败");
        }

    }

    // 处理批量删除请求
    @RequestMapping("/batchDelete")
    @ResponseBody
    public JsonResult deletes(Long[] ids) {
        try {
            if (ids != null) {
                employeeService.batchDelete(ids);

            }
            return new JsonResult(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false, "删除失败");
        }

    }

    // 去新增或去修改页面
    @RequestMapping("/input")
    public String input(Long id, Model model) {

        List<Department> departments = departmentService.selectAll();
        model.addAttribute("departments", departments);

        List<Role> roles = roleService.selectAll();
        model.addAttribute("roles", roles);

        if (id != null) { // 表示去修改
            Employee employee = employeeService.selectByPrimaryKey(id);
            model.addAttribute("employee", employee);
        }
        // 让浏览器重新发一次请求
        return "/employee/input";
    }

    // 新增或修改
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Employee employee, Long[] ids) {
        if (employee.getId() != null) {
            // 修改
            employeeService.updateByPrimaryKey(employee, ids);
        } else {
            //新增
            if (null == employee.getAdmin()) {
                employee.setAdmin(false);
            }
            employeeService.insert(employee, ids);
            // 让浏览器重新发一次请求
        }
        return new JsonResult(true, "操作成功");
    }

    /**
     * 用户名验证操作
     *true:用户名不存在
     *false: 用户名已存在
     * @param name 检查用户名是否存在
     *
     * @return
     */
    //@RequestMapping("/checkName")
    //@ResponseBody
    //public Map checkName(String name,Long id,Model model) {
    //
    //    HashMap<String, Object> map = new HashMap<>();
    //    Employee employee = employeeService.selectByname(name,id);
    //    //model.addAttribute("employee1", employee1);
    //    if (id==null){
    //        map.put("valid", employee == null);
    //    }else {
    //        Employee employee1 = employeeService.selectByPrimaryKey(id);
    //        map.put("valid", (employee == null || employee1.getName().equals(name)));
    //    }
    //    return map;
    //}

    @RequestMapping("/checkName")
    @ResponseBody
    public String checkName(String name, Long id) {
        return JSON.toJSONString(employeeService.selectByname(name, id));

    }


    /**
     * 导出
     *
     * @param response
     * @throws IOException
     */
    @RequestMapping("/exportXls")
    public void exportXls(HttpServletResponse response) throws IOException {
        //设置响应头信息
        response.setHeader("Content-Disposition", "attachment;filename=employee.xls");
        Workbook workbook = employeeService.exportXls();
        //输出内容到响应
        workbook.write(response.getOutputStream());
    }

    /**
     * 导入
     */
    @RequestMapping("/improtXls")
    @ResponseBody
    public JsonResult improtXls(MultipartFile file) {
        employeeService.improtXls(file);
        return new JsonResult(true, "导入成功");
    }


}
